package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.config.keyword.DateUtils;
import com.cskaoyan.catmall.dao.KeywordDao;
import com.cskaoyan.catmall.model.bo.BaseParamBo;
import com.cskaoyan.catmall.model.pojo.keyword.KeywordPojo;
import com.cskaoyan.catmall.model.vo.keyword.GetListVo;
import com.cskaoyan.catmall.model.vo.keyword.KeywordVo;
import com.cskaoyan.catmall.service.KeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/01/07 10:29
 *
 * @author WangZhe
 * 备注:
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordDao keywordDao;

    @Override
    public GetListVo getList(BaseParamBo bo) {
        Integer page = bo.getPage();//页码
        Integer limit = bo.getLimit();//数据量
        String sort = bo.getSort();//排序列
        String order = bo.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<KeywordVo> list = keywordDao.getList(sort, order);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<KeywordVo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GetListVo.data(total, pages, limit, page, list);
    }

    @Override
    public GetListVo getListByKeywordAndUrl(Integer page, Integer limit, String sort, String order, String keyword, String url) {
        PageHelper.startPage(page, limit);

        List<KeywordVo> list = keywordDao.getListByKeywordAndUrl(sort, order, keyword, url);

        PageInfo<KeywordVo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GetListVo.data(total, pages, limit, page, list);
    }

    @Override
    public KeywordVo createKeyWord(KeywordPojo keywordPojo) {
        keywordPojo.setAddTime(DateUtils.formatDate(new Date()));
        keywordPojo.setUpdateTime(DateUtils.formatDate(new Date()));
        keywordDao.createKeyWord(keywordPojo);
        KeywordVo keywordVo = keywordDao.selectOneById(keywordPojo.getId());

        return keywordVo;
    }

    @Override
    public void deleteKeyWord(Integer id) {
        keywordDao.deleteKeyWord(id);

    }

    @Override
    public void updateKeyWord(KeywordPojo keywordPojo) {
        keywordPojo.setUpdateTime(DateUtils.formatDate(new Date()));
        keywordDao.updateKeyWord(keywordPojo);
    }
}
